package com.teddy.customWidgets;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.ExpandBar;
import org.eclipse.swt.widgets.ExpandItem;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Link;
import org.eclipse.swt.widgets.Text;

import com.hp.hpl.jena.rdf.model.Property;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.rdf.model.Statement;
import com.hp.hpl.jena.rdf.model.StmtIterator;
import com.teddy.controller.RDFSchemaControl;
import com.teddy.model.skos.SKOS;
import com.teddy.model.skos.interfaces.SKOSGenerico;
import com.teddy.util.FileManager;
import com.teddy.view.BaseGUI;
import com.teddy.view.tree.ThesaurusTreeITem;

public class ResourcePropertiesGroup extends BasePropertiesGroup{

	private Composite literalComposite;
	private ThesaurusTreeITem item; 
	
	public ResourcePropertiesGroup(Composite parent, int style, ThesaurusTreeITem item) {
		super(parent, style);
		this.item = item;
		GridLayout gridLayout = new GridLayout(2, true);
		gridLayout.horizontalSpacing = 20;
		setLayout ( gridLayout );
		setLayoutData (new GridData(SWT.FILL, SWT.FILL, true, true));
		setText ( RDFSchemaControl.getResourceString( "properties.title") );
	}
	
	public void createContent() {
		if (item != null){
			literalComposite = new Composite( this, SWT.NONE );
			literalComposite.setLayout(new GridLayout(1, false));
			literalComposite.setLayoutData( new GridData(SWT.FILL, SWT.FILL, true, true));
			
			BaseGUI.setResPrefLabel( createPropertiesSingleValue2( literalComposite,
																   "properties.prefLabel", 
																   item.getLiteralProperty(SKOS.prefLabel), 
																   false,
																   SKOS.prefLabel));
			BaseGUI.setResPrefSymbol( createPropertiesSingleValue2( literalComposite, 
																	"properties.prefSymbol", 
																	item.getLiteralProperty(SKOS.prefSymbol), 
																	false,
																	SKOS.prefSymbol));
			BaseGUI.setResAltLabel( createPropertiesSingleValue2( literalComposite, 
																  "properties.altLabel", 
																  item.getLiteralProperty(SKOS.altLabel), 
																  false,
																  SKOS.altLabel));
			BaseGUI.setResAltSymbol( createPropertiesSingleValue2( literalComposite, 
																   "properties.altSymbol", 
																   item.getLiteralProperty(SKOS.altSymbol), 
																   false,
																   SKOS.altSymbol));
			BaseGUI.setResScopeNote( createPropertiesSingleValue2( literalComposite, 
																   "properties.scopeNote", 
																   item.getLiteralProperty(SKOS.scopeNote), 
																   false ));
			BaseGUI.setResDefinition( createPropertiesSingleValue2( literalComposite, 
																    "properties.definition", 
																    item.getLiteralProperty(SKOS.definition), 
																    true,
																    SKOS.definition));
			BaseGUI.setResNotation( createPropertiesSingleValue2( literalComposite, 
																  "properties.notation", 
																  item.getLiteralProperty(SKOS.notation), 
																  true,
																  SKOS.notation));				
			Button submitBtn = new Button( literalComposite, SWT.NONE);
			submitBtn.setText( "Save changes");
			submitBtn.setToolTipText( "Save changes");
			submitBtn.setLayoutData( new GridData( SWT.END, SWT.FILL, false, false));
			Image img = FileManager.getImage( RDFSchemaControl.getResourceString("icon.save") );
			submitBtn.setImage( img );
			submitBtn.setBounds( img.getBounds());
			
			ExpandBar bar = new ExpandBar( this, SWT.V_SCROLL | SWT.H_SCROLL );
			bar.setLayoutData( new GridData( SWT.FILL, SWT.FILL, true, true));
			
			BaseGUI.setResExpandBar( bar );
			
			ArrayList<String> values = new  ArrayList<String>();
			values.add("Prop 1");
			values.add("Prop2");
	
			refreshMultipleValueProperties( bar, item );
			
		}
	}
	
	public void refreshMultipleValueProperties(ExpandBar bar, ThesaurusTreeITem item ){
		BaseGUI.setResInSchema(( createProperties2ColumnMultipleValue( bar, 
																	   "properties.inSchema", 
																		SKOSGenerico.inScheme, 
																		item.getMultiplePropertyValue(SKOS.inScheme), 
																		0, 
																		true, 
																		SKOS.inScheme)));
		BaseGUI.setResHasTopConcept( createProperties2ColumnMultipleValue( bar, 
																		   "properties.hasTopConcept",
																			SKOSGenerico.hasTopConcept, 
																			item.getMultiplePropertyValue(SKOS.hasTopConcept), 
																			1,
																			true, 
																			SKOS.hasTopConcept));
		BaseGUI.setResNarrower( createProperties2ColumnMultipleValue( bar, 
																	  "properties.narrower",
																	  SKOSGenerico.narrower, 
																	  item.getMultiplePropertyValue(SKOS.narrower), 
																	  2, 
																	  true, 
																	  SKOS.narrower));
		BaseGUI.setResBroader( createProperties2ColumnMultipleValue( bar, 
																	 "properties.broader", 
																	 SKOSGenerico.broader, 
																	 item.getMultiplePropertyValue(SKOS.broader), 
																	 3, 
																	 true, 
																	 SKOS.broader));
		BaseGUI.setResRelated( createProperties2ColumnMultipleValue( bar, 
																	 "properties.related", 
																	 SKOSGenerico.related, 
																	 item.getMultiplePropertyValue(SKOS.related), 
																	 4, 
																	 true, 
																	 SKOS.related));
	}
	protected Text createPropertiesSingleValue2( Composite parent, 
												 String labelKey, 
												 String value, 
												 boolean isMultiLine,
												 Property p) {
		final Property prop = p;
		Composite propertyComposite = new Composite( parent, SWT.NONE );
		propertyComposite.setLayout(new GridLayout(1, false));
		propertyComposite.setLayoutData( new GridData(SWT.FILL, SWT.FILL, true, false ));
		
		Label label = new Label( propertyComposite, SWT.NONE );
		label.setText( RDFSchemaControl.getResourceString( labelKey + ".label") );
		label.setToolTipText( RDFSchemaControl.getResourceString( labelKey + ".tooltip" ) );
		
		Text editBox;
		GridData gridData;
		if( isMultiLine ) {
			editBox = new Text( propertyComposite, SWT.WRAP | SWT.MULTI | SWT.NONE );
			gridData = new GridData(SWT.FILL, SWT.FILL, true, false);
			gridData.minimumHeight = 50;
			editBox.setLayoutData( gridData );
		} else {
			editBox = new Text( propertyComposite, SWT.NONE );
			editBox.setLayoutData( new GridData(SWT.FILL, SWT.FILL, true, false));
		}
		editBox.setText(value);
		editBox.setToolTipText( RDFSchemaControl.getResourceString( labelKey + ".tooltip" ) );
				
		return editBox;
	}
	
	protected ExpandItem createProperties2ColumnMultipleValue( final ExpandBar bar, final String keyLabel,
			 final int propertyID,
			 List<Resource> values, 
			 final int index,
			 final boolean isResource,
			 Property p) {

		final Property prop = p;
		Image img;
			
		Composite composite = new Composite (bar, SWT.NONE);
		GridLayout layout = new GridLayout (2, false);
		layout.marginLeft = layout.marginTop = layout.marginRight = layout.marginBottom = 10;
		layout.verticalSpacing = 10;
		composite.setLayout(layout);
		Link link;
		CustomButton button;
			
		for (Resource value : values) {
			link = new Link(composite, SWT.NONE );
			if (isResource){
				link.setText("<A>" + getResourceLabel(value) + "</A>");
			}
			else
				link.setText(value.toString());
			
			button = new CustomButton(composite, SWT.PUSH, value.toString(), propertyID );
			button.setToolTipText("Remove");
			img  = FileManager.getImage( RDFSchemaControl.getResourceString("icon.delete2") );
			button.setImage(img);
			//button.setText(value.toString());
			button.setBounds(img.getBounds());
			button.setToolTipText(value.toString());
			button.addSelectionListener(new SelectionListener() {
					
				@Override
				public void widgetSelected(SelectionEvent e) {
					Button b = (Button) e.widget;
					((ThesaurusTreeITem)BaseGUI.getSelectedItem()).removeFromMultipleResources(prop, b.getToolTipText());
					
					ExpandItem modifiedExpandItem = BaseGUI.getExpandItem(prop);
					modifiedExpandItem.getControl().dispose();
					modifiedExpandItem.dispose();
					
					BaseGUI.setExpandItem(createProperties2ColumnMultipleValue( bar, 
																				 keyLabel, 
																				 propertyID, 
																				 ((ThesaurusTreeITem)BaseGUI.getSelectedItem()).getMultiplePropertyValue(prop), 
																				 index, 
																				 isResource, 
																				 prop), 
															prop);
				}
					
				@Override
				public void widgetDefaultSelected(SelectionEvent e) {
				}
			});
		}
		
		img  = FileManager.getImage( RDFSchemaControl.getResourceString("icon.add") );
			
		button = new CustomButton(composite, SWT.PUSH, "", propertyID );
		button.setToolTipText("Add");
		button.setImage( img);
		button.setBounds(img.getBounds());
			
		button.addSelectionListener(new SelectionListener() {
				
			@Override
			public void widgetSelected(SelectionEvent e) {
			// TODO Auto-generated method stub
				AddConceptRelationDialog dialog = new AddConceptRelationDialog(getShell(), item.getAllResources(), item.getSelectedLanguage());
				
				Resource selectedRes = dialog.open();
				((ThesaurusTreeITem)BaseGUI.getSelectedItem()).addToMultipleResources(prop, selectedRes.getURI());
				
				ExpandItem modifiedExpandItem = BaseGUI.getExpandItem(prop);
				modifiedExpandItem.getControl().dispose();
				modifiedExpandItem.dispose();
				
				BaseGUI.setExpandItem(createProperties2ColumnMultipleValue( bar, 
																			 keyLabel, 
																			 propertyID, 
																			 ((ThesaurusTreeITem)BaseGUI.getSelectedItem()).getMultiplePropertyValue(prop), 
																			 index, 
																			 isResource, 
																			 prop), 
														prop);
			}
				
			@Override
			public void widgetDefaultSelected(SelectionEvent e) {
				// TODO Auto-generated method stub
					
			}
		});
			
		ExpandItem item = new ExpandItem (bar, SWT.NONE, index);
		item.setText( RDFSchemaControl.getResourceString( keyLabel + ".label") );
		item.setHeight(composite.computeSize(SWT.DEFAULT, SWT.DEFAULT).y);
		item.setControl(composite);
		item.setExpanded(true);
			
		return item;
	}
	
	public String getResourceLabel(Resource res){
		StmtIterator it =  res.listProperties(SKOS.prefLabel);
		while(it.hasNext()){
			Statement s = it.next();
				if (s.getLanguage().equals(item.getSelectedLanguage())){
					return s.getString();
			}
		}
		return res.getURI();
	}
}
